/***************************************************************************
 *   Copyright (C) 2007 by Nicholas Robbins, David Murphy                  *
 *   nrrobbin@umcg.org, djmurphy@umcg.org                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <QObject>
#include <QWidget>
#include <QPixmap>
#include <QPainter>
#include <QColor>
#include <QFrame>
#include <QLayout>
#include <QLabel>
#include <QPushButton>
#include <QMouseEvent>
#include "dots.h"
#include <iostream>

using namespace std;

dots::dots()
{}

dots::dots( QWidget * parent, Qt::WindowFlags fl) : QWidget( parent, fl ), buf( 71, 21 )
{
	buf.fill(Qt::transparent);
	x=71;
	h=21;
	//x = this->width();
	//h = this->height();
//	cout << h << "  " << x << endl;
	dragging = false;
}

//dots::~dots()
//{}

/*void dots::mouseMoveEvent( QMouseEvent * me )
{
}*/

void dots::paintEvent(QPaintEvent * pe)
{

	QPixmap temp (buf);
	QPainter p (&temp);
	
	p.setPen(Qt::black);
	
//	p.drawRect(0,0,this->width(),h);
// replace draw rect with draw dots based on math conversion done in mouse release event
/*	if(dragging)
	{
		p.setPen(Qt::gray);
		p.drawRect(0, 0, xi, 14);
	}*/
	
// 	for(int c=0; c<5; c++)
// 	{
// 		p.drawEllipse( (c*((x)/5))+1, 1, 12, 12 );
// 	}
// 	
	num = (xi/13) + 1;
	if(num<1)
		num=1;
	if(num>5)
		num=5;

	for(int c=0; c<5; c++)
	{
		if( c < num )
		{
			p.setBrush(Qt::black);
			p.drawEllipse( (c*((x)/5))+1, 1, 12, 12 );
		}
		else
		{
			p.setBrush(Qt::transparent);
			p.drawEllipse( (c*((x)/5))+1, 1, 12, 12 );
		}
		cout << "num " << num << endl;
//		emit dotsChanged(num);
	}
	
	p.end();
	p.begin(this);
	
	p.drawPixmap (0, 0, temp);
}

void dots::mousePressEvent( QMouseEvent * me )
{
	if( me->button() == Qt::LeftButton )
	{
		dragging = true;
		xi = me->x();
	}
	
	repaint();
}

void dots::mouseReleaseEvent( QMouseEvent * me )
{
	if( me->button() == Qt::LeftButton )
	{
		dragging = false;
		xi = me->x();

/*		if (x2<0) x2=0;
		if (x2 > max) set to max;

		x2 = <math conversion here>;
*/
		QPainter p (&buf);
		QPen mypen = p.pen();

		//<store values in your database>

		repaint();
		emit dotsChanged(num);
	}
//	emit dotsChanged(num);
 }

void dots::mouseMoveEvent( QMouseEvent * me )
{
	if (dragging)
	{
		xi = me->x( );
		if( xi > 70)
			xi=70;
		if(xi < 2)
			xi=2;
	}
		
/*	oldx2 = x2;
	oldy2=((int)((oldy2+7)/15))*15;*/
		
	repaint();
}

int dots::getValue()
{
	return num;
}